package com.zqwh.xtool.model.online;

import android.content.Context;

import com.zqwh.yj.sdk.utils.httpRequest.RetrofitSingleton;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.NetCallback;
import com.zqwh.yj.sdk.utils.httpRequest.subscribers.ProgressSubscriber;
import com.zqwh.yj.sdk.utils.httpRequest.HttpResultFunc2;
import com.zqwh.xtool.api.offline.DeviceResposity;
import com.zqwh.xtool.api.online.DeviceApi;
import com.zqwh.xtool.config.BaseConfig;
import com.zqwh.xtool.domain.DeviceConfigVO;
import com.zqwh.xtool.greendao.db.Device;
import com.zqwh.xtool.model.DeviceModel;

import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class DeviceModelOnlineImpl implements DeviceModel {
    private DeviceApi deviceApi;
    private DeviceResposity deviceService;
    private Context context;

    public DeviceModelOnlineImpl(Context context) {
        this.context = context;
        this.deviceApi = RetrofitSingleton.getInstance(BaseConfig.BASE_URL).create(DeviceApi.class);
        this.deviceService = new DeviceResposity(this.context);
    }

    @Override
    public void activated(String sn, NetCallback<DeviceConfigVO> callback) {
        deviceApi.activated(sn)
                .map(new HttpResultFunc2<DeviceConfigVO>())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber(callback, context, false));
    }

    @Override
    public boolean insertDevice(Device device) {
        return deviceService.insertDevice(device);
    }

    @Override
    public boolean insertMultDevice(List<Device> deviceList) {
        return deviceService.insertMultDevice(deviceList);
    }
}
